package com.pachong.android.framework.citypicker;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.pachong.android.frameworkbase.utils.GsonUtils;
import com.pachong.android.frameworkbase.utils.SLog;
import com.pachong.android.frameworkbase.utils.io.AssetsUtil;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


/**
 * 全国省份城市操作类
 */
public class CityDataSource {

    /**
     * 所有的省市String
     */
//    private static final String FILE_NAME = "fw_city_city_infoes.txt";
    private static final String FILE_NAME = "fwb_province_list.json";
    private final List<Province> provinceList;

    /**
     * 某个省份被选中之后，默认会选中一个市。 市被选中之后默认会选中一个区/乡镇， 这就是三级联动。
     * 这里的cache list是在province被选中之后的缓存
     */
    HashMap<String, List<Province.City>> mCacheCityMap = new HashMap<>();//保存市列表的map
    HashMap<String, List<Province.Country>> mCacheCountyMap = new HashMap<>();//保存县级列表map

    public CityDataSource(Context context) {
        long time1 = System.currentTimeMillis();
        SLog.e("vivi", "time --> " + time1);
        String data = AssetsUtil.getStringFromAssets(context, FILE_NAME);
        SLog.e("vivi", "cost time --> " + (System.currentTimeMillis() - time1));

        provinceList = GsonUtils.string2ObjecetList(data, Province[].class);
    }


    /**
     * 获取省列表名称
     *
     * @return
     */
    List<String> provinceNameList;

    public List<String> getProvinceNameList() {
        if (provinceNameList != null) {
            return provinceNameList;

        } else {
            provinceNameList = new ArrayList<>();
            for (Province province : provinceList) {
                provinceNameList.add(province.name);
                mCacheCityMap.put(province.name, province.cityList);
            }
            return provinceNameList;
        }
    }

    /**
     * 获取市列表名称
     *
     * @param provinceName
     * @return
     */
    public List<String> getCityNameList(String provinceName) {
        List<String> cityNameList = new ArrayList<>();
        List<Province.City> cities = mCacheCityMap.get(provinceName);
        if (cities != null && cities.size() > 0) {
            for (Province.City city : cities) {
                cityNameList.add(city.name);
                mCacheCountyMap.put(city.name, city.countryList);
            }
        } else {
            Log.w("city", "getCityNameList 选中的省份没有城市信息");
        }
        return cityNameList;
    }

    /**
     * 获取县列表名称
     */
    public List<String> getCountyNameList(String cityName) {
        ArrayList<String> countyNameList = new ArrayList<>();
        try {
            if (mCacheCountyMap.get(cityName) != null) {
                List<Province.Country> countryList = mCacheCountyMap.get(cityName);
                for (Province.Country country : countryList) {
                    countyNameList.add(country.name);
                }
            }
        } catch (Exception e) {
            SLog.e("vivi", "cityName = " + cityName + "e " + Arrays.toString(e.getStackTrace()));
            e.printStackTrace();
        }
        return countyNameList;
    }


    /**
     * 获取省份、城市、区 的id
     *
     * @param provinceName
     * @param cityName
     * @param countryName
     * @return
     * @throws JSONException
     */
    public void findIds(String provinceName,
                              Province outProvince,
                              String cityName,
                              Province.City outCity,
                              String countryName,
                              Province.Country outCountry) {
        for (Province province : provinceList) {
            if (TextUtils.equals(province.name, provinceName)) {
                outProvince.id = province.id;
                outProvince.name = province.name;
                if (!TextUtils.isEmpty(cityName)) {
                    for (Province.City city : province.cityList) {
                        if (TextUtils.equals(city.name, cityName)) {
                            outCity.id = city.id;
                            outCity.name = city.name;
                            if (!TextUtils.isEmpty(countryName)) {
                                for (Province.Country country : city.countryList) {
                                    if (TextUtils.equals(country.name, countryName)) {
                                        outCountry.id = country.id;
                                        outCountry.name = country.name;
                                    }
                                }
                            }

                        }
                    }
                }

            }
        }
    }

}
